package com.java.base.exercise.aysn.config;

import com.java.base.exercise.aysn.ThreadPoolEnum;
import com.java.base.exercise.aysn.ThreadPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Executor;

/**
 * @Author RenPu
 * @Date 2023/5/10 11:44
 * @Version 1.0
 * @Description:
 **/
//@Slf4j
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
    @Autowired
    private ThreadPoolUtils threadPoolUtils;

//    Executor DIR_Executor=Runnable::run;

    @Override
    public Executor getAsyncExecutor() {
        return threadPoolUtils.getThreadPool(ThreadPoolEnum.DEFAULT);
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (throwable, method, objects) -> System.out.println("异步任务执行失败: {}"+throwable.getMessage());
    }
}